﻿$(document).ready(function() {

	
	//checking other registration information is correct
	$("form :input.must").each(function() {
		var $star = $("<span class='cred'> * </span>");
		$(this).parent().append($star);
	});

	$("form :input").blur(function() {
		var $parent = $(this).parent();
		$parent.find(".hint").remove();

		if($(this).is("#username")) {
			if(this.value=="" || this.value.length < 8) {
				$(this).removeClass("right");
				$(this).addClass("wrong");
				var errorMsg = "The length is less than 8";
				$parent.append("<span class='hint onError'>"+errorMsg+"</span>");
			}else{
				$(this).removeClass("wrong");
				$(this).addClass("right");
				var okMsg = "Correct user name format";
				$parent.append("<span class='hint onSuccess'>"+okMsg+"</span>");
			}
		if(this.value!="") {
		var u = $(this).val();
		var postPath = getContextPath() + "/DesignerService";
		
			$.post(
                postPath,{username: u,method: "isExistedUser"},function( data ) {
                    if ( data == 0 ) {
                        $( '#usernameResponse' ).css( 'display', 'none' );
                    } else {
                        $( '#usernameResponse' ).css( 'display', 'inline' );

                    }
                }
            );
		/*$.ajax({type: "POST",postPath:u,data:{username: u,method: "isExistedUser"}, success:
				function(responseData) {
			 if ( responseData == 0 ) {
                 $( '#usernameResponse' ).css( 'display', 'none' );
             } else {
                 $( '#usernameResponse' ).css( 'display', 'inline' );

             }
		}});*/
		}
		}


		if($(this).is("#password")) {
			if(this.value=="" || this.value.length < 8) {
				$(this).removeClass("right");
				$(this).addClass("wrong");
				var errorMsg = "The length of password should not be less than 8";
				$parent.append("<span class='hint onError'>"+errorMsg+"</span>");
			}else{
				$(this).removeClass("wrong");
				$(this).addClass("right");
				var okMsg = "Correct password format";
				$parent.append("<span class='hint onSuccess'>"+okMsg+"</span>");
			}
		}
		
		if($(this).is("#confirmpassword")) {
			if($("#password").val() != this.value || $("#password").val() == "") {
				$(this).removeClass("right");
				$(this).addClass("wrong");
				var errorMsg = "It should be the same with the previous password.";
				$parent.append("<span class='hint onError'>"+errorMsg+"</span>");
			}else{
				$(this).removeClass("wrong");
				$(this).addClass("right");
				var okMsg = "Correct password format";
				$parent.append("<span class='hint onSuccess'>"+okMsg+"</span>");
			}
		}


		if($(this).is("#email")) {
			if(this.value=="" || (this.value!="" && !/.+@.+\.[a-zA-Z]{2,4}$/.test(this.value))) {
				$(this).removeClass("right");
				$(this).addClass("wrong");
				var errorMsg = "Wrong email format";
				$parent.append("<span class='hint onError'>"+errorMsg+"</span>");
			}else{
				$(this).removeClass("wrong");
				$(this).addClass("right");
				var okMsg = "Correct email format";
				$parent.append("<span class='hint onSuccess'>"+okMsg+"</span>");
			}
		}

	});/*.keyup(function() {
		$(this).triggerHandler("blur");
	});*/

	$("#submit").click(function() {
		$("form .must:input").trigger("blur");
		var errorNum = $("form .onError").length;
		if(errorNum) {
			document.getElementById("errorDiv").innerHTML = "<font color='red'>"+errorNum+" field(s) are still not correct.</font>";
			return false;
		}
	});
});


function getContextPath() {
    var pathName = document.location.pathname;
    var index = pathName.substr(1).indexOf("/");
    var result = pathName.substr(0,index+1);
    return result;
}